package server.business.task.data;

import java.util.Calendar;
import java.util.List;

import server.ScriptEnum;
import server.business.task.config.TaskConfig;
import server.commonData.User;

/**
 * 任务事件
 * @author King
 *
 */
public class TaskEventsScript implements ITaskEventScript
{
	/* (non-Javadoc)
	 * @see server.business.task.data.ITaskEventScript#changeName(server.commonData.User)
	 */
	@Override
	public void changeName(User user)
	{
		List<Task> tasks = user.getTaskList().getTaskConditionMap().get(TaskEnum.CHANGNAME);
		for(Task task:tasks)
		{
			if(task.isAward())
				continue;
			task.setCanAward(true);
		}
	}

	@Override
	public int getId() {
		return ScriptEnum.TASK_EVENT;
	}

	@Override
	public void onUpdate(User user, long curMill)
	{
		int today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
		for(Task task:user.getTaskList().getTaskList().values())
		{
			TaskConfig config = task.getConfig();
			checkDayRefresh(task, today,config);
			checkTaskEvent(user,task, config);
		}
	}
	
	/**
	 * 检测任务条件
	 * @param task
	 * @param config
	 */
	private void checkTaskEvent(User user,Task task,TaskConfig config)
	{
		if (task.isAward())
			return;
		switch (config.conditionType) 
		{
		case TaskEnum.CHONGZHI:
			if (user.getRechargedGold() < Integer.parseInt(config.conditionPar))
				return;
			break;
		case TaskEnum.JINBI:
			if (user.getMoney() >= Integer.parseInt(config.conditionPar))
				return;
			break;
		case TaskEnum.PLAYER_NUM:
			if ((user.getFailNum() + user.getWinNum()) < Integer.parseInt(config.conditionPar))
				return;
			break;
		case TaskEnum.LEVEL:
			if (user.getLevel() < Integer.parseInt(config.conditionPar))
				return;
			break;
		case TaskEnum.VIPLEVEL:
			if (user.getVipLevel() < Integer.parseInt(config.conditionPar))
				return;
			break;
		default:
			break;
		}
		task.setCanAward(true);
	}
	
	
	/**
	 * 检测每日刷新任务
	 * @param task
	 * @param today
	 */
	private void checkDayRefresh(Task task,int today,TaskConfig config)
	{
		if(!config.refreshByDay)
			return;
		if(task.getLastRefreshDay() == today)
			return;
		task.setAward(false);
		task.setLastRefreshDay(today);
	}
}
